package ru.neoflex.chugunov.gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.NetworkInterface;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import ru.neoflex.chugunov.Scan;
import ru.neoflex.chugunov.ScanServices;
import ru.neoflex.chugunov.Scheduler;
import ru.neoflex.chugunov.Word;
import ru.neoflex.chugunov.database.WordJDBCDAO;
import ru.neoflex.chugunov.input.InputSourceTxt;
import ru.neoflex.chugunov.utils.NetworkHelper;

public class CardsNeoFrame extends JFrame {
	private ScanServices ss;
	private Scan s;
	private int INFORMATION_MESSAGE;

	CardsNeoFrame() {
		initComponents();
	}

	public void initComponents() {

		JFrame frame = new JFrame("CardsNeo");
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setResizable(false);

		Font font = new Font("Verdana", Font.PLAIN, 11);

		JMenuBar menuBar = new JMenuBar();

		JMenu fileMenu = new JMenu("File");
		fileMenu.setFont(font);

		JMenu newMenu = new JMenu("Import");
		newMenu.setFont(font);
		fileMenu.add(newMenu);

		JMenu connMenu = new JMenu("Dictionary");
		connMenu.setFont(font);
		fileMenu.add(connMenu);

		JMenu learnMenu = new JMenu("Learn");
		learnMenu.setFont(font);
		fileMenu.add(learnMenu);

		JMenuItem learnToday = new JMenuItem("Learn today");
		learnToday.setFont(font);
		learnMenu.add(learnToday);

		JMenuItem translateWords = new JMenuItem("TranslateWords");
		translateWords.setFont(font);
		connMenu.add(translateWords);

		JMenuItem txtFileItem = new JMenuItem("Text file");
		txtFileItem.setFont(font);
		newMenu.add(txtFileItem);

		JMenuItem pdfFileItem = new JMenuItem("PDF file");
		pdfFileItem.setFont(font);
		newMenu.add(pdfFileItem);

		fileMenu.addSeparator();

		JMenuItem exitItem = new JMenuItem("Exit");
		exitItem.setFont(font);
		fileMenu.add(exitItem);

		exitItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		learnToday.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				List<Word> dayLearn = new ArrayList<>();
				Scheduler scheduler = new Scheduler();
				dayLearn = scheduler.addDayLearn(scheduler
						.addAllWordsForLearn());
				if (dayLearn.size() > 0) {
					for (Word word : dayLearn) {
						Card.createAndShowUI(word);
					}
				} else {
					JOptionPane.showMessageDialog(null,
							"Don't exist word to learn!", "Information",
							INFORMATION_MESSAGE);
				}
			}
		});

		translateWords.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Scan scan = new Scan();
				WordJDBCDAO jd = new WordJDBCDAO();
				ss = new ScanServices();
				scan.setDict(jd.printAll());
				ss.addScanTranslate(scan);
			}

		});

		txtFileItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				InputSourceTxt is = new InputSourceTxt();
				ss = new ScanServices();
				try {
					s = ss.sortTopWords(is.read());
				} catch (IOException e1) {
					e1.printStackTrace();
				}

				JFrame frameImportWord = new JFrame("Import words");
				JTextArea jTextArea1 = new JTextArea();
				JScrollPane jScrollPane = new JScrollPane();
				JPanel jPanel = new JPanel();
				JButton jButtonAddWords = new JButton("AddWordsInDict");
				JButton jButtonAddTranslate = new JButton("Translate Words");
				jButtonAddWords.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						if (s.getDict() == null) {
							JOptionPane.showMessageDialog(null,
									"Nothing to Add!", "Error",
									INFORMATION_MESSAGE);
						} else {
							ss.addScan(s);
							JOptionPane.showMessageDialog(null,
									"Add Complete!", "Information",
									INFORMATION_MESSAGE);
						}
					}
				});
				jButtonAddTranslate.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						if (NetworkHelper.isOnline()) {
							ss.addScanTranslate(s);
						} else {
							JOptionPane.showMessageDialog(null,
									"Need connect to Internet for translate",
									"Information", INFORMATION_MESSAGE);
						}
					}
				});
				jTextArea1.setColumns(20);
				jTextArea1.setRows(5);
				jScrollPane.setViewportView(jTextArea1);
				jPanel.add(jScrollPane);
				jPanel.add(jButtonAddWords);
				jPanel.add(jButtonAddTranslate);
				frameImportWord.add(jPanel);
				frameImportWord.pack();
				frameImportWord.setVisible(true);

				if (s.getDict() != null) {
					for (int i = 0; i < s.getDict().size(); i++) {
						jTextArea1.append("Word : "
								+ s.getDict().get(i).getWord() + " - "
								+ s.getDict().get(i).getOccurence() + "\n");
					}

				}
			}
		});

		frame.addWindowListener(new WindowListener() {
			public void windowActivated(WindowEvent event) {

			}

			public void windowClosed(WindowEvent event) {

			}

			public void windowClosing(WindowEvent event) {
				Object[] options = { "Yes", "No" };
				int n = JOptionPane.showOptionDialog(event.getWindow(),
						"Exit CardsNeo?", "Confirm exit",
						JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
				if (n == 0) {
					event.getWindow().setVisible(false);
					System.exit(0);
				}
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
			}
		});

		menuBar.add(fileMenu);

		frame.setJMenuBar(menuBar);

		frame.setPreferredSize(new Dimension(270, 225));
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new CardsNeoFrame();
			}
		});
	}
}
